package com.suixun.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suixun.dao.StdObjectinfoMapper;
import com.suixun.dao.studentmanage.StudentManageDAO;
import com.suixun.entity.StdObjectinfo;
import com.suixun.entity.StdObjectinfoExample;
import com.suixun.entity.form.StudentForm;
import com.suixun.entity.page.PageModel;
import com.suixun.service.StudentService;
import com.suixun.util.DateUtil;
import com.suixun.util.StringUtil;
import com.suixun.vo.StudentVO;

import rop.utils.spring.CollectionUtils;

/**
 * 
 * <p>
 * Title: StudentServiceImpl
 * </p>
 * <p>
 * 功能描述:学生信息业务处理
 * </p>
 * 
 * @author 陈显强
 * @date 2016年12月12日下午5:36:37
 * @version 1.0
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentManageDAO studentManageDAO;
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;
	@Resource
	private StdObjectinfoMapper stdObjectinfoMapper;

	/**
	 * （非 Javadoc）
	 * <p>
	 * Title: queryList
	 * </p>
	 * <p>
	 * 功能描述:按学校名称/学生姓名条件分页查询学生信息
	 * </p>
	 * 
	 * @param student
	 * @return
	 * @throws SQLException
	 * @see com.suixun.service.StudentService#queryList(com.suixun.entity.form.StudentForm)
	 */
	@Override
	public PageModel<StudentVO> queryList(StudentForm student) throws SQLException {
		String schoolname = null, studentname = null, simid = null, mdtid = null, objectid = null, isNotPage = null;
		PageModel<StudentVO> pageModel = new PageModel<StudentVO>();
		if (student != null) {
			if (StringUtil.isNotEmpty(student.getSchoolname())) {
				schoolname = student.getSchoolname();
			}
			if (StringUtil.isNotEmpty(student.getStudentname())) {
				studentname = StringUtil.append(student.getStudentname());
			}
			if (StringUtil.isNotEmpty(student.getSimid())) {
				simid = StringUtil.append(student.getSimid());
			}
			if (StringUtil.isNotEmpty(student.getObjectid())) {
				objectid = StringUtil.append(student.getObjectid());
			}
			if (StringUtil.isNotEmpty(student.getIsNotPage())) {
				isNotPage = student.getIsNotPage();
			}
			if (StringUtil.isNotEmpty(student.getObjectid())) {
				objectid = StringUtil.append(student.getObjectid());
			}
			if (StringUtil.isNotEmpty(student.getMdtid())) {
				mdtid = StringUtil.append(student.getMdtid());
			}
			if (StringUtil.isNotEmpty(student.getPagecount())) {
				pageModel.setPagesize(Integer.parseInt(student.getPagecount()));
			}
			if (student.getType() != null) {
				pageModel.getQueryparam().put("type", student.getType());
			}
		}
		pageModel.getQueryparam().put("schoolname", schoolname);
		pageModel.getQueryparam().put("studentname", studentname);
		pageModel.getQueryparam().put("simid", simid);
		pageModel.getQueryparam().put("mdtid", mdtid);
		pageModel.getQueryparam().put("objectid", objectid);
		pageModel.getQueryparam().put("isNotPage", isNotPage);
		if (isNotPage == null) {
			Integer totleRows = studentManageDAO.countRows(pageModel);
			pageModel.setTotlepage(totleRows);
		}
		if (student != null) {
			if (StringUtil.isNotEmpty(student.getPagenumber())) {
				pageModel.setCurrentpage(Integer.parseInt(student.getPagenumber()));
			}
		}
		List<StdObjectinfo> students = studentManageDAO.queryByPaging(pageModel);
		List<StudentVO> studentVOs = new LinkedList<>();
		for (StdObjectinfo objectinfo : students) {
			StudentVO studentVO = new StudentVO();
			studentVOs.add(toStudentVO(objectinfo, studentVO));
			studentVO = null;
		}
		pageModel.setQueryresult(studentVOs);
		return pageModel;
	}

	/**
	 * <p>
	 * Title: queryByMdtid
	 * </p>
	 * <p>
	 * 功能描述:通过设备号查询学生信息
	 * </p>
	 * 
	 * @param student
	 * @return
	 */
	@Override
	public StudentVO queryByParam(StudentForm student) {
		String mdtid = null, simid = null;
		if (StringUtil.isNotEmpty(student.getMdtid())) {
			mdtid = student.getMdtid();
		}
		if (StringUtil.isNotEmpty(student.getSimid())) {
			simid = student.getSimid();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mdtid", mdtid);
		map.put("simid", simid);
		StdObjectinfo objectinfo = studentManageDAO.queryByParam(map);
		if (objectinfo == null) {
			return null;
		}
		StudentVO studentVO = new StudentVO();
		return toStudentVO(objectinfo, studentVO);
	}

	/**
	 * 
	 * <p>
	 * Title: toStudentVO
	 * </p>
	 * <p>
	 * 功能描述:将StdObjectinfo转换为StudentVO供页面展示
	 * </p>
	 * 
	 * @param objectinfo
	 * @param studentVO
	 * @return
	 */
	public StudentVO toStudentVO(StdObjectinfo objectinfo, StudentVO studentVO) {
		studentVO.setObjectid(String.valueOf(objectinfo.getObjectid()));
		studentVO.setBirthday(
				objectinfo.getBirthday() == null ? null : DateUtil.formatDateToString(objectinfo.getBirthday(), null));
		studentVO.setGrade(!StringUtil.isNotEmpty(objectinfo.getGrade()) ? null : objectinfo.getGrade());
		studentVO.setHeight(objectinfo.getHeight() == null ? null : objectinfo.getHeight().toString());
		studentVO.setMdtid(!StringUtil.isNotEmpty(objectinfo.getMdtid()) ? null : objectinfo.getMdtid());
		studentVO.setObjectname(!StringUtil.isNotEmpty(objectinfo.getObjectname()) ? null : objectinfo.getObjectname());
		studentVO.setSchoolname(!StringUtil.isNotEmpty(objectinfo.getSchoolname()) ? null : objectinfo.getSchoolname());
		studentVO.setSex(!StringUtil.isNotEmpty(objectinfo.getSex()) ? null : objectinfo.getSex());
		studentVO.setSimid(!StringUtil.isNotEmpty(objectinfo.getSimid()) ? null : objectinfo.getSimid());
		studentVO.setStudentid(!StringUtil.isNotEmpty(objectinfo.getStudentid()) ? null : objectinfo.getStudentid());
		studentVO.setStudentname(
				!StringUtil.isNotEmpty(objectinfo.getStudentname()) ? null : objectinfo.getStudentname());
		studentVO.setWeight(objectinfo.getWeight() == null ? null : objectinfo.getWeight().toString());
		return studentVO;
	}

	@Override
	public List<StdObjectinfo> getStudentsByMainGuardian(String peopletel) {
		if (StringUtils.isBlank(peopletel)) {
			return null;
		}
		return sqlSessionTemplate.selectList("StdObjectinfoExtendSql.listBabysByMainGuardian", peopletel);
	}

	@Override
	public StdObjectinfo selectStudent(String mdtid) {
		if(StringUtils.isBlank(mdtid)) {
			return null;
		}
		StdObjectinfoExample stdObjectinfoExample = new StdObjectinfoExample();
		stdObjectinfoExample.createCriteria().andMdtidEqualTo(mdtid).andIsdeletedEqualTo(false);
		List<StdObjectinfo> objectList = stdObjectinfoMapper.selectByExample(stdObjectinfoExample);
		if(CollectionUtils.isEmpty(objectList)) {
			return null;
		}else {
			return objectList.get(0);
		}
	}
}
